package com.GB26875_3_2011.message.parser;

import com.GB26875_3_2011.annotation.Parser;
import com.GB26875_3_2011.common.GB26875ComponentType;
import com.GB26875_3_2011.common.GB26875SystemType;
import com.GB26875_3_2011.model.GB26875MessageBody;
import com.GB26875_3_2011.util.ByteUtils;
import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @author 蒋仕国
 * @projectName GB26875_3_211
 * @ClassName MessageBodyParser_02
 * @date 2025-01-13 15:10
 * @Description: TODO []
 */

@Parser(unitId = 2)
public class MessageBodyParser_02 implements MessageBodyParser{
    @Override
    public GB26875MessageBody parseBody(ByteBuf buffer) {
        MessageBody_02 messageBody_02 = new MessageBody_02();
        messageBody_02.setSystemType(GB26875SystemType.getSystemName(buffer.readByte()));
        messageBody_02.setSystemAddressHex(Integer.toHexString(buffer.readByte()));
        messageBody_02.setComponentType(GB26875ComponentType.getSystemName(buffer.readByte()));
        messageBody_02.setComponentAddressHex(ByteUtils.readSomeByteToHex(4,buffer));
        messageBody_02.status(ByteUtils.byteToBitArray(buffer.readByte(),buffer.readByte()));
        messageBody_02.setComponentDescriptor(ByteUtils.readReadableBytes(buffer));
        return messageBody_02;
    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Accessors(chain = true)
    public static class MessageBody_02 implements GB26875MessageBody {
        // 系统类型 1个字节
        private GB26875SystemType systemType;
        // 系统地址 1个字节
        private String systemAddressHex;
        // 部件类型 1个字节
        private GB26875ComponentType componentType;
        // 部件地址 4个字节
        private String componentAddressHex;

        // 测试状态（1有，0无）
        private int test_00;

        // 火警（1有，0无）
        private int fire_01;

        //故障（1有，0无）
        private int fault_02;

        // 屏蔽（1有，0无）
        private int shield_03;

        // 监管（1有，0无）
        private int monitor_04;

        // 开关（1开启，0关闭）
        private int switch_05;

        // 反馈 （1反馈，0无反馈）
        private int feedback_06;

        // 延时 （1.延时 0 未延时）
        private int delay_07;

        // 电源 （1 故障， 0 正常）
        private int power_08;

        // 预留
        private int reserved_09;

        // 预留
        private int reserved_10;

        // 预留
        private int reserved_11;

        // 预留
        private int reserved_12;

        // 预留
        private int reserved_13;

        // 预留
        private int reserved_14;

        // 预留
        private int reserved_15;

        // 部件说明
        private byte[] componentDescriptor;

        public void status(int[] status) {
            if (status.length != 16) {
                throw new RuntimeException("状态数组长度必须为16");
            }
            this.test_00 = status[0];
            this.fire_01 = status[1];
            this.fault_02 = status[2];
            this.shield_03 = status[3];
            this.monitor_04 = status[4];
            this.switch_05 = status[5];
            this.feedback_06 = status[6];
            this.delay_07 = status[7];
            this.power_08 = status[8];
            this.reserved_09 = status[9];
            this.reserved_10 = status[10];
            this.reserved_11 = status[11];
            this.reserved_12 = status[12];
            this.reserved_13 = status[13];
            this.reserved_14 = status[14];
            this.reserved_15 = status[15];
        }
    }
}
